#include "tree_2_4.h"
#include <cstring>
#include <iostream>

using namespace std;

Lesson::Lesson ( const Lesson &data)
{
	this->courseId = data.courseId;
	this->courseName = data.courseName;
	this->grade = data.grade;
	this->nextLesson = NULL;
}

Lesson::Lesson ( string courseId, string courseName, int grade)
{
	this->courseId = courseId;
    this->courseName = courseName;
	this->grade = grade;
	this->nextLesson = NULL;
}

void Lesson::Print ( void )
{
	cout << "\tCourseName: " << courseName << "\tCourseid: " << courseId << "\tGrade: " << grade << endl;
}

LessonList::LessonList ( void )
{
	start = NULL;
	count = 0;
}


void LessonList::AddLesson ( Lesson lesson )
{
	if ( this->Empty() )
	{
		start = new Lesson ( lesson );
	}
	else
	{
		Lesson *last = start;

		while ( last->Next() != NULL )
		{
			last = last->Next();
		}

		Lesson *newLesson = new Lesson ( lesson );
		last->SetNext ( newLesson );
	}

	this->count++;
}

void LessonList::Print ( void )
{
	Lesson *last;

	if ( start == NULL )
	{	
		return;
	}
	else
	{
		last = start;
	}

	while ( last != NULL )
	{
		last->Print();
		last = last->Next();
	}
}
void LessonList::Delete ( string courseId )
{
	Lesson *targetLesson = start;
	Lesson *previous = NULL;

	while ( targetLesson != NULL && targetLesson->CourseId() != courseId)
	{
		previous = targetLesson;
		targetLesson = targetLesson->Next();
	}

	if ( targetLesson == NULL)
		return;
	else
	{
		if ( previous == NULL )
		{
			start = NULL;
		}
		else
		{
			previous->SetNext ( targetLesson->Next() );
		}
	
		delete targetLesson;
		count--;
	}
}
LessonList::~LessonList ( void )
{
	Lesson *target, *lesson;
	target = lesson = start;

	while ( count )
	{
		lesson = target->Next();
		delete target;
		target = lesson;

		count--;
	}
}
